import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.SwingConstants;

import java.awt.Color;
import java.awt.Font;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JSeparator;
import javax.swing.JTextArea;

public class VueConsultPeriodique extends Vue{

	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JTextField textFieldIssn;
	private JTextField textFieldNom;
	
	private Periodique perioActuel;
	
	
	// pour que les boutons soient des attributs, il faut faire "convert local to field"
	private JButton buttonRech;
	private JButton buttonAnnuler;
	private JButton buttonFermer;
	private JTextArea textAreaParutions;
	
	public VueConsultPeriodique(Controleur controleur) {
		super(controleur);
		setTitle("Informations sur un Periodique");
		setBounds(100, 100, 452, 515);
		contentPane = new JPanel();
		this.addWindowListener (new WindowAdapter() {
			public void windowClosing (WindowEvent e) {
			getControleur().fermerVue(VueConsultPeriodique.this);
			}
		});
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Issn");
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel.setBounds(17, 66, 61, 15);
		contentPane.add(lblNewLabel);
		
		textFieldIssn = new JTextField();
		textFieldIssn.setBounds(112, 64, 114, 19);
		contentPane.add(textFieldIssn);
		textFieldIssn.setColumns(10);
		

		
		buttonAnnuler = new JButton("Annuler");
		buttonAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			getControleur().fermerVue(VueConsultPeriodique.this);}
		});
		buttonAnnuler.setBounds(255, 98, 107, 25);
		contentPane.add(buttonAnnuler);
		
		buttonRech = new JButton("Rechercher");
		buttonRech.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().rechPeriodique(textFieldIssn.getText());
				buttonFermer.setEnabled(true);
				}
		});
		buttonRech.setBounds(255, 61, 107, 25);
		contentPane.add(buttonRech);
		
		buttonFermer = new JButton("Fermer");
		buttonFermer.setEnabled(false);
		buttonFermer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			getControleur().fermerVue(VueConsultPeriodique.this);}
		});
		buttonFermer.setBounds(183, 438, 107, 25);
		contentPane.add(buttonFermer);
		
		textFieldNom = new JTextField();
		textFieldNom.setEditable(false);
		textFieldNom.setColumns(10);
		textFieldNom.setBounds(113, 134, 245, 19);
		contentPane.add(textFieldNom);
		
		JLabel lblNewLabel_2 = new JLabel("Nom");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_2.setBounds(17, 137, 61, 15);
		contentPane.add(lblNewLabel_2);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(77, 164, 270, 2);
		contentPane.add(separator);
		
		JLabel lblParutionDisponibleEn = new JLabel("Parution(s) disponible(s) en consultation");
		lblParutionDisponibleEn.setHorizontalAlignment(SwingConstants.CENTER);
		lblParutionDisponibleEn.setBounds(68, 177, 279, 35);
		contentPane.add(lblParutionDisponibleEn);
		
		textAreaParutions = new JTextArea();
		textAreaParutions.setEditable(false);
		textAreaParutions.setBounds(112, 228, 4, 22);
		JScrollPane scrollPane = new JScrollPane(textAreaParutions);
		scrollPane.setBounds(59, 228, 323, 133);
		contentPane.add(scrollPane);

		}
	/**
	 * alimentation des composants d'affichage en consultant directement l'ouvrage
	 */
	public void alimente(Periodique per) {
			perioActuel = per;
			textFieldNom.setText(per.getNom());
			textAreaParutions.setText("");
			for(String s : per.getParutions().keySet())
			{
				textAreaParutions.append(s + "\n");
			}
	}
	/**
	 * définition des états de la fenêtre, cad des régions actives selon l'état
	 */
	public void setEtat (int etat){
		switch (etat) {
		case initiale: {
			buttonRech.setEnabled(true);
			buttonAnnuler.setEnabled(true);
			buttonFermer.setEnabled(false);
			textFieldNom.setText("");
			textFieldIssn.setText("");
			textAreaParutions.setText("");
			break;
			}
		case finale: {
			buttonRech.setEnabled(false);
			buttonAnnuler.setEnabled(false);
			buttonFermer.setEnabled(true);
			break;
			}
		}
	}
	
	public void update(Observable observable, Object object)
	{
		System.out.println("Updated!");
		alimente(perioActuel);
		super.update(observable, object);
	}
}
